package com.wss.module.market.ui.goods.cart.adapter;

import com.wss.common.base.adapter.BaseListAdapter;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.base.adapter.superadapter.SuperViewHolder;
import com.wss.common.utils.ImageUtils;
import com.wss.module.market.ResourceTable;
import com.wss.module.market.bean.GoodsInfo;
import com.wss.module.market.bean.Vendor;
import com.wss.module.market.utils.ShoppingCartUtils;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.app.Context;
import org.jetbrains.annotations.NotNull;

import java.util.List;


public class OrderSettlementGoodsAdapter extends BaseListAdapter<Vendor> {

    private OnGoodsItemClickListener onGoodsItemClickListener;

    private Context context;

    /**
     * 常规列表重写该方法
     *
     * @param context  context
     * @param list     数据源
     * @param onGoodsItemClickListener Item点击回调
     */
    public OrderSettlementGoodsAdapter(Context context,
                                       List<Vendor> list,
                                       OnGoodsItemClickListener onGoodsItemClickListener) {
        super(context,
                list,
                ResourceTable.Layout_market_item_of_order_goods_vendor,
                null);
        this.context = context;
        this.onGoodsItemClickListener = onGoodsItemClickListener;
    }

    @Override
    public void onBindData(@NotNull SuperViewHolder holder, int layoutPosition, @NotNull Vendor data) {
        holder.setText(ResourceTable.Id_tv_vendor_name, data.getVendorName());
        if (data.getGoodsInfos().size() == 1) {
            //一种商品
            GoodsInfo goodsInfo = data.getGoodsInfos().get(0);
            holder.setVisibility(ResourceTable.Id_rl_only_goods, Component.VISIBLE);
            ImageUtils.loadImage(holder.findViewById(ResourceTable.Id_iv_goods), goodsInfo.getGoodsMasterImg());
            holder.setText(ResourceTable.Id_tv_goods_name, goodsInfo.getGoodsName());
            holder.setText(ResourceTable.Id_tv_goods_price, String.format("¥%s", goodsInfo.getGoodsPrice()));
            holder.setText(ResourceTable.Id_tv_goods_count, String.format("x%s", goodsInfo.getNum()));
        } else {
            //多种商品
            holder.setVisibility(ResourceTable.Id_rl_many, Component.VISIBLE);
            holder.setText(ResourceTable.Id_tv_goods_total_count, String.format("共%s件", ShoppingCartUtils.calculationGoodsCount(data.getGoodsInfos())));
            ListContainer recyclerView = holder.findViewById(ResourceTable.Id_rv_goods_list);
            recyclerView.setItemProvider(new OrderSettlementGoodsInfoAdapter(context, data.getGoodsInfos()));
            if (onGoodsItemClickListener != null) {
                holder.setOnClickListener(ResourceTable.Id_ll_more_goods_list,
                        v -> onGoodsItemClickListener.onMoreGoodsClick(data, layoutPosition));
            }
        }
    }

    /**
     * 商品Item点击事件回调
     */
    public interface OnGoodsItemClickListener {

        /**
         * 更多商品点击
         *
         * @param data     供应商
         * @param position position
         */
        void onMoreGoodsClick(Vendor data, int position);
    }
}
